//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Base/Include/Common.h"
#include <md5/md5.h>

namespace Engine
{
namespace Base
{

	//
	// MD5 Hash
	//

	struct MD5Hash
	{
	// types
	public:
		typedef static_array< uint8, 16 >		hash_t;


	// variables
	private:
		MD5_CTX		_context;
		hash_t		_hash;
		bool		_changed;


	// methods
	public:
		MD5Hash () : _changed(false)
		{
			_Reset();
		}

		explicit
		MD5Hash (const uni_binbuffer_t &data) : _changed(false)
		{
			Compute( data );
		}

		void Compute (const uni_binbuffer_t &data)
		{
			_Reset();
			Append( data );
		}

		void Append (const uni_binbuffer_t &data)
		{
			_changed = true;
			MD5_Update( &_context, (void *)data.ptr(), data.Size() );
		}

		hash_t const & Get ()
		{
			if ( _changed )
			{
				MD5_Final( _hash.ptr(), &_context );
				_changed = false;
			}
			return _hash;
		}


	private:
		void _Reset ()
		{
			_changed = true;
			MD5_Init( &_context );
		}
	};

}	// Base
}	// Engine